﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.warehouse
{
    public partial class stockOutOrderList : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
        readonly DALStockOrder theOrder = new DALStockOrder();
        readonly DALStockOrderGoods theOrderGoods = new DALStockOrderGoods();
        tbStockOrder oOrder;
        DataTable table;
        StringBuilder sbGoodsList = new StringBuilder();
        StringBuilder sbProcessRemark = new StringBuilder();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(ERule.StockOutOrderList);
                this.BindOrderStatesList();
                this.BindSupplyCompanyList();
                this.BindGrid();
            }

        }

        private void BindGrid()
        {
            DataTable table = GetGridData();
            Grid1.DataSource = table;
            Grid1.DataBind();

        }

        string GetWhereSql
        {
            get
            {
                StringBuilder sbSQL = new StringBuilder(" 1=1 ");
                if (this.ddlOrderStates.SelectedItem.Value != "-1")
                {
                    sbSQL.AppendFormat(" and isnull(stockStates,0)={0} ", this.ddlOrderStates.SelectedItem.Value);
                }
                if (this.txtDateFrom.Text.Trim() != "")
                {
                    sbSQL.AppendFormat(" and goodsToDate >= '{0}' ", this.txtDateFrom.Text.ToString());
                }
                if (this.txtDateTo.Text.Trim() != "")
                {
                    sbSQL.AppendFormat(" and goodsToDate <= '{0}' ", this.txtDateTo.Text.ToString());
                }
                if (this.txtKeywords.Text.Trim() != "")
                {
                    sbSQL.AppendFormat(" and (stockOrderCode like '%{0}%' or linker like '%{0}%' or linkerPhone like '%{0}%' )"
                                         , this.txtKeywords.Text.Trim()
                                         );
                }
                if (this.ddlCompanyList.SelectedItem.Value != "-1")
                {
                    sbSQL.AppendFormat(" and stockCompanyId={0} ", this.ddlCompanyList.SelectedItem.Value);
                }
                sbSQL.AppendFormat(" and companyId=0 ");

                return sbSQL.ToString();
            }
        }


        void BindSupplyCompanyList()
        {
            DataTable table = theCompany.GetList("").Tables[0];
            this.ddlCompanyList.DataTextField = "name";
            this.ddlCompanyList.DataValueField = "id";
            this.ddlCompanyList.DataSource = table;
            this.ddlCompanyList.DataBind();
            this.ddlCompanyList.Items.Insert(0, new ExtAspNet.ListItem("选择采购商", "-1"));
            this.ddlCompanyList.SelectedValue = "-1";

        }

        void BindOrderStatesList()
        {
            this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("新订单", "0"));
            this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("出库中", "1"));
            this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("已完成", "2"));
            this.ddlOrderStates.Items.Add(new ExtAspNet.ListItem("所有订单", "-1"));
            this.ddlOrderStates.SelectedValue = "0";
        }


        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = GetGridData();
            DataView view1 = table.DefaultView;
            view1.Sort = String.Format("{0} {1}", e.SortField, e.SortDirection);
            Grid1.DataSource = view1;
            Grid1.DataBind();
        }

        DataTable GetGridData()
        {
            DataTable table = theOrder.GetList(GetWhereSql).Tables[0];
            DataTable tbGoods;
            if (table != null)
            {
                DataColumn dc = new DataColumn("goodsList", System.Type.GetType("System.String"));
                table.Columns.Add(dc);
                DataColumn dc1 = new DataColumn("statesName", System.Type.GetType("System.String"));
                table.Columns.Add(dc1);

                foreach (DataRow drOrder in table.Rows)
                {
                    tbGoods = theOrderGoods.GetGoodsListByOrderCode(drOrder["stockOrderCode"].ToString()).Tables[0];

                    sbGoodsList = new StringBuilder();
                    foreach (DataRow dr in tbGoods.Rows)
                    {

                        sbGoodsList.AppendFormat("<tr><td>{0}</td><td>{1}</td><td>{2}</td><td>{3}</td><td>{4}</td><td class='con_right'>{5}</td><td>{6}</td><td>{7}</td></tr>"
                                                , dr["goodsCode"].ToString()
                                                , dr["goodsName"].ToString()
                                                , dr["modelTypeName"].ToString()
                                                , dr["goodsStockPrice"].ToString()
                                                , dr["goodsCount"].ToString()
                                                , dr["sumOrderPrice"].ToString()
                                                , dr["outWarehouseCount"].ToString()
                                                , string.Format("<a  onclick=\"openModelWindow('{0}',{1},{2},{3},{4},{5})\" style='cursor:pointer;'>[出库]</a>"
                                                                 , drOrder["stockOrderCode"].ToString()
                                                                 , dr["goodsId"].ToString()
                                                                 , dr["goodsStockPrice"].ToString()
                                                                  , dr["modelTypeId"].ToString()
                                                                  , drOrder["id"].ToString()
                                                                  , dr["goodsCount"].ToString()
                                                                 )
                                                );
                    }
                    drOrder["goodsList"] = sbGoodsList.ToString();

                    if (drOrder["stockStates"].ToString() == "0")
                    {
                        drOrder["statesName"] = "<div class='new_order'>新订单</div>";
                    }
                    else if (drOrder["stockStates"].ToString() == "1")
                    {
                        drOrder["statesName"] = "<div class='new_inhousewareing'>出库中</div>";
                    }
                    else if (drOrder["stockStates"].ToString() == "2")
                    {
                        drOrder["statesName"] = "<div class='new_complate'>已完成</div>";
                    }

                }

            }
            return table;

        }



        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Window1_Close(object sender, EventArgs e)
        {
            this.BindGrid();
        }

        protected void Grid1_RowDataBound(object sender, ExtAspNet.GridRowEventArgs e)
        {
            if (this.Grid1.DataKeys[e.RowIndex][2].ToString() == "2")
            {

                this.Grid1.Rows[e.RowIndex].Values[8] = "";

            }
        }

    }
}